var https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cateList: [],
    cate_id: 0,
    goodslist: [],
    keywords: ''
  },
  // 分类页
  changeColums() {
    wx.navigateTo({
      url: '/pages/shopping_classify/classify',
    })
  },
  /**
   * 分类切换
   */
  chanageCate(obj) {
    var cate_id = obj.currentTarget.dataset.cate_id
    this.setData({
      cate_id: cate_id,
      goodslist: [],
      page: 0,
      keywords: ''
    })
    this.getGoods();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getCate();
    that.getGoods();
  },
  /**
   * @xk 获取分类
   */
  getCate: function () {
    var that = this;
    https.post('getCate', {}, true,'加载中').then(res => {
      that.setData({
        cateList: res
      })
    })
  },
  /**
   * @xk 获取商品
   */
  getGoods: function () {
    var that = this,
      cate_id = this.data.cate_id ? this.data.cate_id : 0,
      page = this.data.page ? this.data.page : 0,
      user_id = wx.getStorageSync('user_id'),
      goodslist = this.data.goodslist ? this.data.goodslist : [],
      keywords = this.data.keywords ? this.data.keywords : '';
    https.post('getGoods', {
      cate_id: cate_id,
      page: page,
      user_id: user_id,
      keywords: keywords,
      is_page: 1
    },true,'正在加速获取').then(res => {
      if(res.list){
        goodslist.push.apply(goodslist,res.list);
      }
      that.setData({
        goodslist: goodslist,
        goodsCartCount: res.goodsCartCount,
        page: res.page
      })
    })
  },
  /**
   * @xk 搜索
   */
  search: function(obj){
    let keywords = obj.detail.value;
    if(keywords != ''){
      this.setData({
        keywords: keywords,
        goodslist: [],
        page: 0,
        cate_id: 0
      })
      this.getGoods();
    }
  },
  show: function(obj){
    var goods_id = obj.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goods_info/goods_info?goods_id='+goods_id,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      goodslist: [],
      cate_id: 0,
      page: 0,
      keywords: ''
    })
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getGoods();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var timestamp = Date.parse(new Date()),
    app_name = wx.getStorageSync('app_name'),
    app_logo = wx.getStorageSync('app_logo'),
    user_id = wx.getStorageSync('user_id'),
    url = '/pages/home/home',
    timestr = timestamp / 1000;
    url += '?timestr=' + timestr;
    if (user_id > 0) {
      url += '&l_id=' + user_id;
    }
    return {
      title: app_name,
      path: url,
      imageUrl: app_logo
    }
  }
})